package com.guochenglang.www.controller;

import com.guochenglang.www.pojo.Member;
import com.guochenglang.www.service.MemberService;
import com.guochenglang.www.service.UserService;
import com.guochenglang.www.util.GetObject;
import com.guochenglang.www.util.MyDependency;
import com.guochenglang.www.util.ObjectTurnKey;
import com.guochenglang.www.view.vo.MemberView;
import com.guochenglang.www.web.result.Result;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;


/**
 * 成员Controller层
 *
 * @author GuoChenglang
 */

public class MemberController {
    private static final MemberService MEMBER_SERVICE = MyDependency.returnObject(MemberService.class);
    private static final UserService USER_SERVICE = MyDependency.returnObject(UserService.class);

    /**
     * 获得知识库成员
     */
    public static Result baseMember(HttpServletRequest request, HttpServletResponse response) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        //获得知识库id
        int id = Integer.parseInt(request.getParameter("baseId"));
        //根据用户id获得用户并返回前端显示
        ArrayList<Member> members = MEMBER_SERVICE.baseMemberList(new Member(null, id, null, null));
        ArrayList<MemberView> memberViews = new ArrayList<>();
        for (Member member : members) {
            MemberView memberView = new MemberView();
            String username = USER_SERVICE.getUser(member.getMemberId()).getUsername();
            memberView.setUsername(username);
            memberView.setMember(member);
            memberViews.add(memberView);
        }
        return Result.result(memberViews);
    }

    /**
     * 删除成员
     */
    public static Result deleteMember(HttpServletRequest request, HttpServletResponse response) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException, IOException {
        //获得成员id
        Integer memberId = GetObject.getValue(request, Integer.class, "memberId");
        //获得执行人id
        Integer userId = GetObject.getValue(request, Integer.class, "userId");
        int num = MEMBER_SERVICE.deleteMember(memberId, userId);
        //根据num返回执行结果
        if (num < 0) {
            return Result.noResult();
        } else {
            return Result.result(num);
        }
    }

    /**
     * 获得我的成员信息
     */
    public static Result myMember(HttpServletRequest request, HttpServletResponse response) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        //获得用户id
        int userId = Integer.parseInt(request.getParameter("userId"));
        //获得知识库id
        int baseId = Integer.parseInt(request.getParameter("baseId"));
        Member conditions = new Member(null, baseId, null, userId);
        return Result.result(MEMBER_SERVICE.baseMemberList(conditions).get(0));
    }

    /**
     * 改变成员状态
     */
    public static Result changeMember(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        //将设置的数据封装
        Member newMember = new Member(GetObject.getValue(request, Integer.class, "id"), GetObject.getValue(request, Integer.class, "baseId"), GetObject.getValue(request, String.class, "memberStatus"), GetObject.getValue(request, Integer.class, "memberId"));
        //获得用户id
        int userId = GetObject.getValue(request, Integer.class, "userId");
        int num = MEMBER_SERVICE.changeMember(newMember, userId);
        Result result = Result.result(num);
        //根据num返回执行结果
        if (num > 0) {
            return result;
        } else {
            return Result.noResult();
        }
    }

}
